#include "../include/udp.h"
#include "../include/queue.h"
#include <pthread.h>  
//#include "thread_pool.h"


// void *  process (SQueue *queue)  
// {  
//     printf ("threadid is 0x%ld, working on task %d\n", pthread_self (),queue->elem->msg.id); 
//     if (sockfd == NULL)
//     {
//         printf("server socket is null...\n");
//     } 

//     sendto(sockfd, info, SEND_MSGSIZE, 0, (struct sockaddr *)&queue->elem->client, sin_size);
//     //sleep (1);/*休息一秒，延长任务的执行时间*/  
//     return NULL;  
// }  


/**
* @file observer.h
* @func receiver
* @explain  接受client消息
* @date 
*/
void receiver(int port, SQueue *queue)
{
    struct sockaddr_in server; /* server's address information */
    struct sockaddr_in client; /* client's address information */
    socklen_t sin_size;
    int num;

    /*** create socket ***/
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        /* handle exception */
        perror("Creating socket failed.");
        exit(-1);
    }

    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(port);
    server.sin_addr.s_addr = htonl (INADDR_ANY);

    /*** bind socket ***/
    if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) 
    {
        /* handle exception */
        perror("Bind error.");
        exit(-1);
    } 

    /***** 初始化消息队列 ******/
    Elem *pelem = (Elem *)malloc(sizeof(Elem));


    sin_size = sizeof(struct sockaddr_in);
    char recvdata[DATA_MAXSIZE];
    char send[DATA_MAXSIZE];
    while (1) 
    {
        memset(recvdata, 0, DATA_MAXSIZE);
        memset(send, 0, DATA_MAXSIZE);

        num = recvfrom(sockfd, recvdata, DATA_MAXSIZE, 0, (struct sockaddr *)&client, &sin_size); 

        if (num < 0)
        {
            perror("recvfrom error\n");
            exit(1);
        }

        printf("Hi, I'am recved...\n");
        /*****  入队 *****/
        memcpy(&pelem ->msg, recvdata, sizeof(MSG));
        memcpy(&pelem ->client, &client, sizeof(client));

        printf("Hi, I'am memcpy sucessed...msg.data:%s\n", pelem ->msg.data);

        if (Queue_Enqueue(queue, pelem) < 0){
            printf("insert queue failed...\n");
            return;
        }

        printf("lengh:%d\n", Queue_Length(queue));
        Queue_Traverse(queue);

        if(!strncmp(pelem ->msg.data, "quit", 4)) 
        {
           // char result[] = {"server quit..."};
           // sendto(sockfd, result, strlen(result), 0, (struct sockaddr *)&client, sin_size);
            break;
        }
        
        // sprintf(send, "send count:%d; time:%s; data:%s; data lengh:%d\n", recvmsg.id, recvmsg.time, recvmsg.data, recvmsg.lengh);
        // sendto(sockfd, send, strlen(send), 0, (struct sockaddr *)&client, sin_size);

    }

    close(sockfd);      /* close listenfd */ 
    return;
}



int main()
{
    strncpy(info, "hello I'am recved...", SEND_MSGSIZE);
    sin_size = sizeof(struct sockaddr_in);
    SQueue queue;
    q = &queue;
    
    //1.初始化线程池
    Queue_Init(&queue, 3);
    //2.接受client消息并入队列
    
    receiver(9999, &queue);

    //3.从队列中取出消息供线程消化

    //4.所有线程去排队轮巡竞争一个消息队列，每条消息需要互斥锁（即只能供一个线程处理）
    return 0;
}